package com.example.mercury;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mercury.entity.Game;
import com.example.mercury.entity.User;
import com.example.mercury.game.FirstStageActivity;
import com.example.mercury.game.GameStartActivity;
import com.example.mercury.game.SecondStageActivity;
import com.example.mercury.game.ThirdStageActivity;
import com.example.mercury.main.FlowTagActivity;
import com.example.mercury.main.SelectPopupWindowActivity;
import com.example.mercury.utils.HttpUtils;
import com.example.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Activity activity;
    private SharedPreferences userInfo;

    private EditText search;
    private Button gototree;

    private HttpUtils httpUtils = new HttpUtils();
    private JsonUtils jsonUtils = new JsonUtils();

    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userInfo = getSharedPreferences("user", 0);
        SharedPreferences.Editor editor = userInfo.edit();
        editor.putString("userInfo", "{\"success\":true,\"code\":20000,\"message\":\"成功\",\"data\":{\"user\":{\"userId\":\"beae50d2-5a6b-4eb4-a498-18e18e97b7cc\",\"userTel\":\"qwe@qq.omc\",\"userNickname\":\"Test2\",\"userFavouriteCoffee\":null,\"userImg\":\"https://graph.baidu.com/pcpage/similar?originSign=1216bd7a94ed338dd79ef01635164538&srcp=crs_pc_similar&tn=pc&idctag=tc&sids=10004_10512_10522_10919_10913_11006_10924_10905_10018_10901_10941_10906_11013_10970_10968_10974_11031_17851_17071_18009_18100_17201_17202_18301_18310_18330_19300_19199_19162_19220_19210_19212_19215_19217_19218_19255_19230_19268_19281_19550_19660_19670_9999_10000&logid=2753907589&entrance=general&tpl_from=pc&pageFrom=graph_upload_pcshitu&image=https%3A%2F%2Fgraph.baidu.com%2Fthumb%2Fv4%2F120482536%2C1109830618.jpg&carousel=503&index=26&page=5&shituToken=db4663\",\"userSweet\":null,\"userBitterness\":null,\"userAcidity\":null,\"userAlcohol\":null,\"userAftertaste\":null,\"wantNum\":0,\"tastedNum\":0,\"coffeeBean\":0}}}");
        editor.commit();

        activity = MainActivity.this;

        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));

        search = findViewById(R.id.et_main_search);
        search.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, FlowTagActivity.class);
                startActivity(intent);
            }
        });
        search.setFocusable(false);


        gototree = findViewById(R.id.btn_gototree);
        String userId = user.getData().getUser().getUserId();
        //String userId = "10bc96d2-6003-430a-95f2-710209e67e0a";

        gototree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Call call = httpUtils.getCall("/tree/tree/"+userId);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String res = response.body().string();
                        Log.e("Main", "onResponse: "+res );
                        Game game = jsonUtils.getGame(res);
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                SharedPreferences.Editor editor = userInfo.edit();
                                editor.putString("tree", res);
                                editor.commit();

                                if (game.getData().getTree() == null){
                                    Intent intent = new Intent(activity, GameStartActivity.class);
                                    startActivity(intent);
                                }else if(game.getData().getTree().getTreeStatus() == 0){
                                    Intent intent = new Intent(activity, FirstStageActivity.class);//第一阶段
                                    startActivity(intent);
                                }else if(game.getData().getTree().getTreeStatus() == 1){
                                    Intent intent = new Intent(activity, SecondStageActivity.class);//第二阶段
                                    startActivity(intent);
                                }else if(game.getData().getTree().getTreeStatus() == 2){
                                    Intent intent = new Intent(activity, ThirdStageActivity.class);//第三阶段
                                    startActivity(intent);
                                }
                            }
                        });
                    }
                });
            }
        });

//        search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View view, boolean b) {
//                if(hasWindowFocus()){
//                    Intent intent = new Intent(MainActivity.this, FlowTagActivity.class);
//                    startActivity(intent);
//                }
//                search.clearFocus();
//            }
//        });

    }
}